var width = $(window).width();
var startNum=0;
var pageSize=20;
var totalPage=1;
var currentPage=1;
$(function() {
	var requestPath = "getBabys.htm?startNum="+startNum+"&pageSize="+pageSize;
	$.ajax({
		type : 'GET',
		contentType : 'application/json',
		url : requestPath,
		dataType : 'json',
		success : function(data) {
			view.item = data.result;
			totalPage=data.totalPage;
			var template = $('#box-template').val();
			var html = Mustache.to_html(template, view);
			
			//alert(html);
			$('#list').append(html);
			$newElems = $('#list').css({ opacity: 0 });
			var $list = $("#list");
			$list.imagesLoaded(function() {
				$list.masonry({
					itemSelector : "#list .box",
					columnWidth : 22,
					isFitWidth: true
				});
				$list.animate({ opacity: 1 });
			});
		},
	});

	var bindScrollEvent = function() {
		var _self = this;
		$(window).bind('scroll', function() {
			var h = MANLOST.tool.checkIsInView($('#getMore'));
			if (h <= 50) {
				if (!view.hold) {
					startNum+=pageSize;
					currentPage+=1;
					if(currentPage<=totalPage){
						getMore();
					}
				}
			}
		});
	}
	var getMore = function() {
		var requestPath = "getBabys.htm?startNum="+startNum+"&pageSize="+pageSize;
		view.hold = true;
		$.ajax({
			type : 'GET',
			contentType : 'application/json',
			url : requestPath,
			dataType : 'json',
			success : function(data) {
				view.item = data.result;
				var template = $('#box-template').val();
				var html = Mustache.to_html(template, view);
				var $boxes = $(html);
				$('#list').imagesLoaded(function() {
					$('#list').append($boxes).masonry('appended', $boxes,true);
				});
				/*
				 * $('#list').append( $boxes1); $container=$('#list'); $boxes =
				 * $boxes1.css({ opacity: 0 });
				 * 
				 * $boxes.imagesLoaded(function(){ $container.masonry(
				 * 'appended', $boxes, true ); // 渐显新的内容 $boxes.animate({
				 * opacity: 1 }); // 隐藏正在加载模块 $("#page_loading").hide("fast");
				 * });
				 */

			},
		});
		view.hold = false;
	}

	bindScrollEvent();
});
